// 获取当前定位城市
import request from './request'
export default function getCurrentCity ()  {
  // test 1

  // let localCity = JSON.parse(localStorage.getItem('city'))
  // let current = {}
  // if (!localCity) { // 当前没有数据
  //   // 百度api获取数据
  //   var myCity = new window.BMap.LocalCity()
  //   myCity.get(async data => {
  //     const { data: res } = await request('/area/info','GET', {params: {name: data.name}})
  //     current = res.body.label
  //     localStorage.setItem('city', JSON.stringify(current))
  //   })
  // } else { // 有数据
  //   current = localCity
  // }
  // return current
  
  // test 2 promise
  let localCity = JSON.parse(localStorage.getItem('city'))
  if (!localCity) { // 当前没有数据
      // 百度api获取数据
      return new Promise((resolve, reject) => {
        var myCity = new window.BMap.LocalCity()
        try {
          myCity.get(async data => {
            const { data: res } = await request(`/area/info?name=${data.name}`,'GET')
            const current = res.body
            localStorage.setItem('city', JSON.stringify(current))
            // resolve暴露出去
            resolve(current) // 通过.then 获取
          })
        } catch (error) {
          reject(error)
        }
      }) 
    } else { // 有数据
       return Promise.resolve(localCity)
    }
}